package com.itheima.day12_多线程.Test5;

public class Testbox {
    public static void main(String[] args) {
        //生产者线程
        new Thread(() -> {
            int count = 0;
            //1.死循环
            while (true) {
                //2.上锁
                synchronized ("奶箱") {
                    //3.判断
                    if (Box.count == 0) {
                        break;
                    } else {
                        //3.1判断是否有奶
                        if (!Box.flag) {
                            count++;
                            System.out.println("送奶工正在送第"+count+"瓶奶");
                            //3.2给出提示
                            Box.flag = true;
                            //3.3 唤醒
                            "奶箱".notify();
                        } else {
                            //3.4 有奶就等待
                            try {
                                "奶箱".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();

        new Thread(() -> {
            int count = 0;
            while (true) {
                synchronized ("奶箱") {
                    if (Box.count == 0) {
                        break;
                    }else{
                        //判断
                        if(Box.flag){
                            count++;
                            System.out.println("小美同学正在喝第"+count+"瓶奶");
                            Box.count--;
                            Box.flag= false;
                            "奶箱".notify();
                        }else{
                            //没奶 等待
                            try {
                                "奶箱".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
        ).start();
    }
}
